#include<stdio.h>
#include<pthread.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<sys/types.h>
#include<sys/socket.h>
int main(int argc, char **argv)
{
     printf("wait connct\n");
    int socketfd, ret,socketfd2;
    fd_set fd;

    socketfd = socket(PF_INET, SOCK_STREAM, 0);
    socketfd2 = socket(PF_INET, SOCK_STREAM, 0);
    //ret = select(socketfd + 1, &fd, NULL, NULL, NULL);
    struct sockaddr_in saddr;
    memset(&saddr, 0, sizeof(saddr));
    saddr.sin_family = PF_INET;
    saddr.sin_port = htons(50003);
    saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
     printf("wait connct 2\n");
    int res = connect(socketfd, (struct sockaddr *)&saddr, sizeof(struct sockaddr));
    printf("res %d \n", res);
    saddr.sin_port = htons(50004);
    res = connect(socketfd2, (struct sockaddr *)&saddr, sizeof(struct sockaddr));
   
    printf("res %d \n", res);
   
    printf("wait connct\n");
    char buf[50], buf1[50];
    volatile struct	timeval timeover = {0};
    int	nsel;
    timeover.tv_sec = 1;
    timeover.tv_usec = 500000;
    FD_SET(0, &fd);
    FD_SET(socketfd, &fd);
    while(1) {
        timeover.tv_sec = 3;
        timeover.tv_usec = 0;
        
        select(socketfd2 + 1, &fd, NULL, NULL, NULL);
        
        if(FD_ISSET(socketfd, &fd)) {
            if(recv(socketfd, buf1, sizeof(buf1), 0) < 0) {
                perror("recv() error!\n");
            }
            printf("1 %s\n", buf1);
            bzero(buf1, 50);
        }else {
            printf("1 %s\n", "not select");
        }

        if(FD_ISSET(socketfd2, &fd)) {
            if(recv(socketfd2, buf1, sizeof(buf1), 0) < 0) {
                perror("recv() error!\n");
            }
            printf("2 %s\n", buf1);
            bzero(buf1, 50);
        }else {
            printf("2 %s\n", "not select");
        }
    }
}